﻿using M_Quality.API.Application.Command.QualitygroupCommand;
using M_Quality.API.Application.Command.QualityProgromCommand;
using M_Quality.Domain;
using M_Quality.Repository.Interface;
using MediatR;

namespace M_Quality.API.Application.CommandHandler.QualityProgromCommandHandler
{
    public class QualityProgromCreateCommandHandler : IRequestHandler<QualityProgromCreateCommand, int>
    {
        protected readonly IQualityPlanRepositry QualityPlanRepositry;
        protected readonly IQualityprogromRepositry QualityprogromRepositry;

        public QualityProgromCreateCommandHandler(IQualityPlanRepositry qualityPlanRepositry, IQualityprogromRepositry qualityprogromRepositry)
        {
            QualityPlanRepositry = qualityPlanRepositry;
            QualityprogromRepositry = qualityprogromRepositry;
        }

        public async Task<int> Handle(QualityProgromCreateCommand request, CancellationToken cancellationToken)
        {

            await QualityPlanRepositry.AddAsync(new QualityPlan()
            {
                QualityPlanCode = request.QualityPlanCode,
                QualityPlanName = request.QualityPlanName,
                QualityPlanSart = request.QualityPlanSart,
               

            });
            foreach (var item in request.QualityinspectionId)
            {
                await QualityprogromRepositry.AddAsync(new Qualityprogram()
                {
                    QualityPlanCode = request.QualityPlanCode,
                    QualitygroupId = request.QualitygroupId,
                    QualityinspectionId = item
                  });
            }
            return 1;


    }
    }
}
